﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace nwrole.Module.lib
{
    public class ModuleSettings : IModuleSettings
    {
        protected string _filename;
        protected XmlDocument _xmlDocument;

        private bool _changed;

        public ModuleSettings(string xmlFilename)
        {
            _filename = xmlFilename;

            Load();
        }

        public void Reload()
        {
            Load();
        }

        private void Load()
        {
            _xmlDocument = new XmlDocument();

            try
            {
                _xmlDocument.Load(_filename);
                _changed = false;
            }
            catch
            {
                _xmlDocument.LoadXml("<Settings></Settings>");
                _changed = true;
            }
        }

        public void Save()
        {
            if(_changed)
            {
                //_xmlDocument.Save(_filename);

                XmlWriterSettings writerSettings = new XmlWriterSettings { Indent = true };
                XmlWriter writer = XmlWriter.Create(_filename, writerSettings);
                _xmlDocument.Save(writer);

                _changed = false;
            }
        }


        public int GetSetting(string xPath, int defaultValue)
        {
            return Convert.ToInt32(GetSetting(xPath, Convert.ToString(defaultValue)));
        }

        public string GetSetting(string xPath, string defaultValue)
        { 
            XmlNode xmlNode = _xmlDocument.SelectSingleNode("Settings/" + xPath );
            if(xmlNode != null)
            {
                return xmlNode.InnerText;
            }

            return defaultValue;
        }

        public void PutSetting(string xPath, int value)
        {
            PutSetting(xPath, Convert.ToString(value));
        }

        public void PutSetting(string xPath, string value)
        {
            XmlNode xmlNode = _xmlDocument.SelectSingleNode("Settings/" + xPath);

            if(xmlNode == null)
            {
                xmlNode = CreateMissingNode("Settings/" + xPath);
                xmlNode.InnerText = value;
                _changed = true;
            }
            else
            {
                if(xmlNode.InnerText != value)
                {
                    xmlNode.InnerText = value;
                    _changed = true;
                }
            }

            //_xmlDocument.Save(documentPath);
        }

        public bool HasSetting(string xPath)
        {
            XmlNode xmlNode = _xmlDocument.SelectSingleNode("Settings/" + xPath);
            if (xmlNode != null)
                return true;

            return false;
        }

        private XmlNode CreateMissingNode(string xPath)
        {
            string[] xPathSections = xPath.Split('/');
            string currentXPath = "";
            XmlNode testNode;
            XmlNode currentNode = _xmlDocument.SelectSingleNode("Settings");
            foreach (string xPathSection in xPathSections)
            {
                currentXPath += xPathSection;
                testNode = _xmlDocument.SelectSingleNode(currentXPath);
                if (testNode == null)
                {
                    currentNode.InnerXml += "<" +
                                            xPathSection + "></" +
                                            xPathSection + ">";
                }
                currentNode = _xmlDocument.SelectSingleNode(currentXPath);
                currentXPath += "/";
            }
            return currentNode;
        }
    }

}
